package com.hncu.test;

public class Demo1 {//用StringBuilder拼接字符串

    public static void main(String[] args) {
        int[] arr = {11, 22, 33, 44, 55};//创建数组
        System.out.println(connect(arr));//传数组
    }

    public static StringBuilder connect(int[] arr) {
        StringBuilder sb = new StringBuilder();//无参构造一个StringBuilder对象
        //开始加格式
        sb.append("[");
        //遍历数组
        for (int i = 0; i < arr.length; i++) {
            if (i == arr.length - 1) {
                sb.append(arr[i]);
            } else {
                sb.append(arr[i]).append(",");//不是最后一个元素要加上","
            }
        }
        //加格式
        sb.append("]");
        //String s = sb.toString();//将StringBuilder转换成String，让其内容不可改变
        //return s;
        //或者返回StringBuilder
        return sb;
    }
}
